package neiltwist.cfd.lightweightakka;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

public class Image extends JPanel {
   private BufferedImage image;

   public Image() {
       image = new BufferedImage(512, 512, BufferedImage.TYPE_INT_ARGB);

       Graphics g = image.getGraphics();
       g.setColor(new Color(0x00000000));
       g.fillRect(0, 0, 512, 512);
   }
   
   public void reset(){
//	   image.flush();
	   Graphics g = image.getGraphics();
	   g.dispose();
	   g.create();
//       g.setColor(new Color(0x00000000));
//       g.fillRect(0, 0, 512, 512);
   }

   @Override
   protected void paintComponent(Graphics g) {
       g.drawImage(image, 0, 0, null);
   }

   public void process(int x, int y, float nval) {

       nval = nval < 0.0f ? 0.0f : (nval > 1.0f ? 1.0f : nval);

//       nval = (nval + 1.0) / 2.0;
       
       int rval = (int) (nval * 255.0f);
       int gval = (int) (nval * 255.0f);
       int bval = (int) (nval * 255.0f);
       
//       System.out.println("colour is "+nval+" * 255 = "+(nval * 255.0f));

       int colour = 0xff000000 | (rval << 16) | (gval << 8) | bval;

       image.setRGB(x, y, colour);
       
   }
   
   public void processRedLine(int x, int y,int x_diff, int y_diff) {

       if(x<0)
    	   x=0;
       if(x>512)
    	   x=512;
       
       if(y<0)
    	   y=0;
       if(y>512)
    	   y=512;
       
       Graphics g = image.getGraphics();
       g.setColor(Color.RED);
       g.drawLine(x, y, x_diff, y_diff);
       
//       image.setRGB(x, y, colour);
       
       
   }
}
